package com.wwz.leetcode;

import org.junit.Test;

public class LeetCode866 {

    @Test
    public void test() {
        System.out.println(primePalindrome(6));
    }

    public int primePalindrome(int num) {

        if(num==1){
            return 2;
        }

            // 无线循环相当于 while(true)
            for (; ;) {
                boolean isPrime = true, isPalindrome = true;
                // 判断是否为素数
                for (int i = 2, len = (int) Math.sqrt(num); i <= len; ++i) {
                    if (num % i == 0) {
                        isPrime = false;
                        break;
                    }
                }

                // 是否回文
                boolean validhuiwen = validhuiwen(num);

                if (validhuiwen&&isPrime) {
                    return num;
                }
                num++;
            }
    }

    // 是否回文
    public boolean validhuiwen(Integer num) {
        String s = num.toString();
        int left = 0;
        int right = s.length() - 1;
        while (left<=right){
            if(s.charAt(left) == s.charAt(right)){
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }
}
